package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.RoleMenuMapper;
import com.todod.model.RoleMenu;
import com.todod.service.IRoleMenuService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-11-23
 */
@Service
@Transactional
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

	@Override
	public void saveData(Long _userId, Long roleId, List<Long> menuIds) {
		deleteByRoleId(_userId, roleId);

		for (Long menuId : menuIds) {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setId(SnowflakeIdGenerator.getId());
			roleMenu.setCreateTime(LocalDateTime.now());
			roleMenu.setCreateUserId(_userId);

			roleMenu.setMenuId(menuId);
			roleMenu.setRoleId(roleId);

			save(roleMenu);
		}
	}

	@Override
	public void deleteByRoleId(Long _userId, Long roleId) {
		QueryWrapper<RoleMenu> wrapper = new QueryWrapper<RoleMenu>();
		wrapper.eq("rm_role_id", roleId);

		remove(wrapper);
	}

	@Override
	public void deleteByMenuId(Long _userId, Long menuId) {
		QueryWrapper<RoleMenu> wrapper = new QueryWrapper<RoleMenu>();
		wrapper.eq("rm_menu_id", menuId);

		remove(wrapper);
	}

	@Override
	public List<RoleMenu> getByRoleId(Long roleId) {
		QueryWrapper<RoleMenu> wrapper = new QueryWrapper<RoleMenu>();
		wrapper.eq("rm_role_id", roleId);

		return list(wrapper);
	}

}
